package com.ghsc.ghxc.jsb.ui.live.view.cust

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.ext.*
import com.cn.appcore.utils.GlideUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustSignKey
import com.ghsc.ghxc.jsb.databinding.ImCustShareLiveViewBinding
import com.ghsc.ghxc.jsb.ui.live.bean.UserInfoTagAentBean
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean
import com.netease.yunxin.kit.chatkit.ui.normal.view.message.viewholder.NormalChatBaseMessageViewHolder

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.message.yunxin.view
 * 类名：CusRedAttachmentViewHolder
 * 创建时间：2023/10/24 21:10
 * 创建人 @author： xiaolin
 * 描述 ：Todo...分享直播间
 */
class CusShareLiveAttachmentViewHolder(parent: ChatBaseMessageViewHolderBinding, viewType: Int) : NormalChatBaseMessageViewHolder(
    parent, viewType
) {

    private var binding //创建自己的layout布局
            : ImCustShareLiveViewBinding? = null
    override fun addViewToMessageContainer() {
        binding = ImCustShareLiveViewBinding.inflate(
            LayoutInflater.from(parent.context), messageContainer, true
        )
    }

    //去掉背景
    override fun onMessageBackgroundConfig(messageBean: ChatMessageBean?) {
        baseViewBinding.messageContainer.setBackgroundResource(R.color.title_transfer)
    }



    override fun bindData(message: ChatMessageBean?, lastMessage: ChatMessageBean?) {
        super.bindData(message, lastMessage)
        currentMessage = message
        val bean = message!!.messageData.message.attachment as CusShareLiveAttachment
            ?: return
        binding?.let {
            it.title.text="     "+bean.title
            GlideUtil.ImageLoad(parent.context,bean.imgCover,it.imgCover)
            it.tvLiveName.text="直播人："+bean.nickName
            var lsit = arrayListOf<UserInfoTagAentBean>()
//            if (!TextUtils.isEmpty(bean.tags)){
//                val tags :MutableList<Tag> = Gson().fromJson(bean.tags,object : TypeToken<MutableList<Tag>>() {}.type)
//                if (tags.size>0){
//                    for (i in tags.indices)
//                        lsit.add(UserInfoTagAentBean("","",1,tags[i].image))
//                }
//            }
//            if (!TextUtils.isEmpty(bean.agent)){
//                val agent :MutableList<Agent> = Gson().fromJson(bean.agent,object : TypeToken<MutableList<Agent>>() {}.type)
//                if (agent.size>0){
//                    for (i in agent.indices)
//                        lsit.add(UserInfoTagAentBean(agent[i].name,agent[i].agent_level.toString(),2,""))
//                }
//            }
            if (lsit.isNotEmpty()){
                it.llTagAgent.visibility=View.VISIBLE
                val mAdapter = ListAdapter()
                it.rvListTagAgent.adapter = mAdapter
                mAdapter.replaceData(lsit)
            }else it.llTagAgent.visibility=View.GONE
            //主播头像昵称
            GlideUtil.ImageLoad(parent.context,bean.headImg,it.imgHead)
            it.tvName.text=bean.nickName


            it.llItem.setSingleClickListener {
                RxBus.getDefault().post(
                    RxBusBean(
                        CustSignKey.CHAT_LIVE_URL,
                        CustSignKey.CHAT_LIVE_URL, bean)
                )
            }

//            val text = bean.nickName+"正在直播，欢迎来观看 "+bean.liveUrl
//            copyToClipboard(this,text)

//            it.llItem.setOnLongClickListener {
//                val text = bean.nickName+"正在直播，欢迎来观看 "+bean.liveUrl
//                copyToClipboard(parent.context,text)
//                true
//            }

        }

    }
    inner class ListAdapter :
        QuickAdapter<UserInfoTagAentBean>(R.layout.tag_agent_item, convert = { helper, item ->
            if (item.type==1){
                //是imgTag
                helper.setGone(R.id.imgTag,false)
                    .setGone(R.id.tvAgent,true)
                GlideUtil.ImageLoad(parent.context,item.imgTag,helper.getView(R.id.imgTag))
            }else{
                helper.setGone(R.id.imgTag,true)
                    .setGone(R.id.tvAgent,false)

                helper
                    .setText(R.id.tvAgent,item.name)
                when (item.agentLevel) {
                    "1" -> helper.setBackgroundResource(R.id.tvAgent,R.drawable.shape_tag_sheng)
                    "2" -> helper.setBackgroundResource(R.id.tvAgent,R.drawable.shape_tag_shi)
                    "3" -> helper.setBackgroundResource(R.id.tvAgent,R.drawable.shape_tag_qu)
                }

            }


        })
}